// pages/xxx/xxx.vue 中的 script 内容
import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';

export default {
  components: {
    CustomTabBar
  },
  data() {
    return {
      showCustomTabBar: false
    };
  },
  computed: {
    isTabBarPage() {
      const app = getApp();
      const userRole = app.globalData.userRole || '';

      const patientTabPages = [
        'pages/home/home',
        'pages/assessment/health',
        'pages/assessment/risk',
        'pages/consultation/index',
        'pages/my/profile'
      ];

      const doctorTabPages = [
        'pages/doctor/home',
        'pages/doctor/patients',
        'pages/consultation/index',
        'pages/doctor/statistics',
        'pages/my/profile'
      ];

      const pages = getCurrentPages();
      if (pages.length > 0) {
        const currentRoute = pages[pages.length - 1].route;
        console.log('当前页面路径:', currentRoute, '用户角色:', userRole);
        return userRole === 'doctor'
          ? doctorTabPages.includes(currentRoute)
          : patientTabPages.includes(currentRoute);
      }
      return false;
    }
  },
  onLoad() {
    uni.hideTabBar();
    this.updateTabBarState();
  },
  onShow() {
    uni.hideTabBar();
    this.updateTabBarState();
    setTimeout(() => {
      this.updateTabBarState();
    }, 300);
  },
  onReady() {
    this.updateTabBarState();
  },
  methods: {
    updateTabBarState() {
      const app = getApp();
      const userRole = app?.globalData?.userRole || '';

      console.log('更新tabBar状态 - 角色:', userRole, '是tabBar页面:', this.isTabBarPage);

      if (app?.globalData?.useCustomTabBar && this.isTabBarPage) {
        this.showCustomTabBar = true;

        const pages = getCurrentPages();
        if (pages.length > 0) {
          const currentRoute = pages[pages.length - 1].route;
          uni.$emit('route-change', '/' + currentRoute);
        }

        uni.hideTabBar();
      } else {
        if (!this.isTabBarPage) {
          this.showCustomTabBar = false;
        }
      }
    },
    // 已移除 DOM 操作，推荐使用样式处理
    addSafeAreaClass() {
      // 不再使用 querySelector，建议通过模板绑定类名并写在 style 中
      console.log('安全区域通过样式添加，无需 JS 操作');
    }
  }
};
